package com.unism.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.StringUtils;


/**
 * @author zhengbing
 * @date 2011-8-9 下午01:24:25
 * @version 2.0
 * @summary 
 */
public class ExcelExportHelper {
    
    /**
     * Logger for this class
     */
    private static final Log logger = LogFactory.getLog(ExcelExportHelper.class);
    
    @SuppressWarnings("unchecked")
    public static String Export(List list,String rootPath,String fileName,Class clazz){
        Object obj=null;
        if(list != null && list.size() != 0) {
            obj = list.get(0);
        }

        List columnList = new ArrayList();
       
        for(; clazz != Object.class ; clazz = clazz.getSuperclass()) {
            setValues(columnList,clazz,obj,null);
        }
        
        return print(list,columnList,rootPath,fileName);
    }
    
    @SuppressWarnings("unchecked")
    public static String Export(List list,String rootPath,String fileName,Class clazz,String[] displayColumns){
       
        if(list == null || list.size() == 0) return null;
        
        Object obj = list.get(0);
        
        List columnList = new ArrayList();
       
        for(; clazz != Object.class ; clazz = clazz.getSuperclass()) {
            setValues(columnList,clazz,obj,displayColumns);
        }
        
        return print(list,columnList,rootPath,fileName);
    }
    
    @SuppressWarnings("unchecked")
    private static String print(List list,List columnList,String rootPath,String fileName){
        int length = columnList.size();
        
        String[] heads=new String[length];
        String[] itemNames = new String[length];
        String[] outputType = new String[length];
        String[] converter = new String[length];
        int[] width = new int[length + 1];
        
        //设置列宽度
        width[0] = 10;
        for(int i=1;i<width.length;i++){
            width[i] = 20;
        }
        
        //设置输出类型
        for(int i=0;i<outputType.length;i++){
            outputType[i] = "String";
        }
        
        //设置格式字符串
        for(int i=0;i<converter.length;i++){
            converter[i] = "";
        }
                
        for(int i=0;i<columnList.size();i++){
            Map map = (Map) columnList.get(i);
            for(Iterator it = (Iterator) map.keySet().iterator();it.hasNext();){
                String key = (String) it.next();
                String value = (String) map.get(key);
                heads[i] = value;
                itemNames[i] = key;
            }
        }
         
        ExcelHandle.writeExcel(true,rootPath,fileName,heads,itemNames,outputType,width,list,converter);
        return fileName;
    }
    
    @SuppressWarnings("unchecked")
    private static void setValues(List columnList,Class clazz,Object obj,String[] displayColumns){
        System.out.println("columnList.size()="+columnList.size());
        System.out.println("clazz="+clazz);
        System.out.println("obj="+obj);
        System.out.println("displayColumns="+displayColumns);
       
        List list;
        if(displayColumns != null && displayColumns.length > 0)
            list = Arrays.asList(displayColumns);
        else
            list = new ArrayList();
        
        Field[] fields = clazz.getDeclaredFields();
        for(int i=0;i<fields.length;i++){
            
            if(logger.isInfoEnabled()){
                logger.info("字段名称为："+fields[i].getName());
                logger.info("字段类型为："+fields[i].getType().getName());
            }
           
            //判断需要打印的列头数量
            try {
                String methodName = "get"+StringUtils.capitalize(fields[i].getName());
                Method method = clazz.getMethod(methodName);
                Column annotation4Column = method.getAnnotation(Column.class);
                if(annotation4Column != null){
                    Map<String,String> map = new HashMap<String,String>();
                    map.put(fields[i].getName(), annotation4Column.comment());
                    if(isDisplayColumn(fields[i].getName(),list))
                        columnList.add(map);
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    @SuppressWarnings("unchecked")
    /**
     * @columnName 要打印的字段，这里指的是实体bean的属性名称
     * @list 预打印的字段列表，即实体bean的属性集合
     * @return 是否需要打印
     */
    private static boolean isDisplayColumn(String columnName,List list){
        if(list.size() == 0) return true;
        return list.contains(columnName);
    }
}
